<?php
/**
 * @property Payment $receipt
 */
?>
<?php $this->pageTitle = 'Receipt ' . '#' . $receipt->payment_number; ?>
<?php
$this->breadcrumbs = array(
	'Receipts' => array('/receipt/index'),
	'#' . $receipt->payment_number
);
?>
<div class="main-title no-border clearfix">
	<h1 class="float-left">Receipt #<?php echo $receipt->payment_number; ?></h1>
	<?php if ($receipt->deleted == 0): ?>
		<div class="controls">
			<a class="link-button with-icon" href="<?php echo $this->createUrl('/receipt/update', array('id' => $receipt->id)); ?>">
				<img src="/images/icons/fugue/pencil.png" />
				Edit
			</a>
			<?php if ($userAgency->getSetting('EnableTemporaryReceipt') == '1' && $userAgency->getSetting('defaultReceiptPrint') == 'TemporaryReceipt'): ?>
				<a class="link-button with-icon split-button" href="<?php echo $this->createUrl('/receipt/print', array('id' => $receipt->id, 'type' => Receipt::PRINT_TYPE_TEMPORARY)); ?>" target="_blank">
					<img src="/images/icons/fugue/printer.png" />
					Print Receipt
				</a>	
			<?php else: ?>
				<a class="link-button with-icon split-button" href="<?php echo $this->createUrl('/receipt/print', array('id' => $receipt->id, 'type' => Receipt::PRINT_TYPE_OFFICIAL)); ?>" target="_blank">
					<img src="/images/icons/fugue/printer.png" />
					Print Receipt
				</a>
			<?php endif; ?>

			<?php
			$this->widget('PsActionMenu', array(
				'buttonText' => '<span class="caret"></span>',
				'showIndicator' => false,
				'htmlOptions' => array(
					'class' => 'link-button split-button-select'
				),
				'actions' => array(
					'printOfficialReceipt' => array(
						'label' => 'Print Official Receipt',
						'icon' => '/images/icons/fugue/document-invoice.png',
						'url' => array('/receipt/print', 'id' => $receipt->id, 'type' => Receipt::PRINT_TYPE_OFFICIAL),
						'target' => '_blank',
					),
					'printTemporaryReceipt' => array(
						'label' => 'Print Temporary Receipt',
						'icon' => '/images/icons/fugue/document-invoice.png',
						'url' => array('/receipt/print', 'id' => $receipt->id, 'type' => Receipt::PRINT_TYPE_TEMPORARY),
						'target' => '_blank',
						'visible' => ($userAgency->getSetting('EnableTemporaryReceipt') == '1'),
					),
				)
			));
			?>
			<?php if (Yii::app()->user->checkModuleAccess('Propsage.Accounting.Commission')): ?>

				<?php if ($receipt->hasPayableItemsGenerated()): ?>
					<a class="link-button with-icon split-button" href="<?php echo $this->createUrl('/commissionPayable/index', array('PayableItem[receipt_number]' => $receipt->payment_number, 'PayableItem[payment_id]' => $receipt->id)); ?>">
						<img src="/images/icons/farmfresh/16x16/magnifier.png" />
						View Generated Commissions
					</a>	
				<?php else: ?>
					<a class="link-button with-icon split-button" href="<?php echo $this->createUrl('/receipt/calculateCommissions', array('id' => $receipt->id)); ?>">
						<img src="/images/icons/farmfresh/16x16/calculator.png" />
						Calculate Commissions
					</a>
				<?php endif; ?>


				<?php
				if ($receipt->hasPayableItemsGenerated()) {
					$this->widget('PsActionMenu', array(
						'buttonText' => '<span class="caret"></span>',
						'showIndicator' => false,
						'htmlOptions' => array(
							'class' => 'link-button split-button-select'
						),
						'actions' => array(
							'calculateCommissions' => array(
								'label' => 'Re-Calculate Commissions',
								'icon' => '/images/icons/farmfresh/16x16/arrow_rotate_anticlockwise.png',
								'url' => array('/receipt/calculateCommissions', 'id' => $receipt->id),
							),
						)
					));
				}
				?>
			<?php endif; ?>


			<?php if (!$receipt->isPaymentCleared()): ?>
				<a class="link-button with-icon" href="<?php echo $this->createUrl('/receipt/cleared', array('id' => $receipt->id, 'clearedStatus' => 1)); ?>">
					<img src="/images/icons/farmfresh/16x16/accept.png" />
					Mark as Cleared
				</a>
			<?php else: ?>
				<a class="link-button with-icon" href="<?php echo $this->createUrl('/receipt/cleared', array('id' => $receipt->id, 'clearedStatus' => 0)); ?>">
					<img src="/images/icons/farmfresh/16x16/stop.png" />
					Mark as Not Cleared
				</a>
			<?php endif; ?>

		</div>
	<?php else: ?>
		<h1 class="float-left red pad">(Canceled)</h1>
	<?php endif; ?>
	<?php if ($receipt->deleted == 0): ?>
		<div class="controls float-right">
			<a class="link-button with-icon" href="<?php echo $this->createUrl('/receipt/cancel', array('id' => $receipt->id)); ?>">
				<img src="/images/icons/fugue/cross-circle.png" />
				Cancel Receipt
			</a>
		</div>
	<?php endif; ?>
</div>
<div class="flat_area">
	<div class="">
		<div id="ps-payment-view" class="relative">
			<script type="text/javascript">
				var currency_code = "<?php echo (isset($receipt->currency_code) ? $receipt->currency_code : '$'); ?>";
			</script>
			<div id="addReceipt" class="">
				<div class="grid_11">
					<div class="section">
						<div class="block">
							<h2>Receipt Information</h2>
							<?php
							$this->widget('zii.widgets.CDetailView', array(
								'data' => $receipt,
								'nullDisplay' => '-',
								'attributes' => array(
									'payer_name',
									array(
										'name' => 'payment_method_type_code',
										'value' => $receipt->getPaymentMethod(),
									),
									'payment_ref_no',
									'comment'
//									array(
//										'name' => 'payment_date',
//										'type' => 'raw',
//										'value' => Yii::app()->dateFormatter->formatDateTime(strtotime($receipt->payment_date), "short", null)
//									),
//									array(
//										'label' => 'Amount',
//										'value' => Yii::app()->numberFormatter->formatCurrency($receipt->amount, $receipt->currency_code)
//									),
								),
							));
							?>
						</div>
					</div>
				</div>
				<div class="grid_5">
					<div class="section highlighted">
						<div class="block">
							<?php
							$this->widget('zii.widgets.CDetailView', array(
								'data' => $receipt,
								'attributes' => array(
									array(
										'label' => 'Agency',
										'value' => $receipt->ownerParty->getAgency()->name,
										'visible' => Yii::app()->user->hasMultipleAgenciesSelected()
									),
									array(
										'label' => 'Receipt #',
										'value' => $receipt->payment_number
									),
									array(
										'label' => 'Status',
										'type' => 'raw',
										'value' => $receipt->getStatus()
									),
									array(
										'label' => 'Reason for canceling',
										'type' => 'raw',
										'value' => $receipt->delete_reason,
										'visible' => $receipt->deleted
									),
									array(
										'name' => 'payment_date',
										'type' => 'raw',
										'value' => Yii::app()->dateFormatter->formatDateTime(strtotime($receipt->payment_date), "short", null)
									),
									array(
										'label' => 'Amount',
										'value' => Yii::app()->numberFormatter->formatCurrency($receipt->amount, $receipt->currency_code)
									),
								),
								'htmlOptions' => array('class' => 'summary-table')
							));
							?>
						</div>
					</div>
				</div>
				<div id="invoices-applied" class="grid_16">
					<div class="section">
						<div class="block">
							<h2>Invoices Applied</h2>
							<?php
							$this->widget('PsGridView', array(
								'id' => 'payment_application_grid',
								'dataProvider' => $this->getPaymentAppliedInvoiceDataProvider($receipt->id),
								'template' => '{pager}{items}',
								'pagesizeCssClass' => 'pager-pagesize',
								'itemsCssClass' => 'items clear',
								'emptyText' => 'No invoices found.',
								'selectableRows' => 0,
								'htmlOptions' => array(
									'class' => 'grid-view grey',
								),
								'columns' => array(
									'invoice_date' => array(
										'name' => 'invoice_date',
										'type' => 'raw',
										'value' => 'Yii::app()->dateFormatter->formatDateTime(strtotime($data->invoice_date), "short", null)',
										'headerHtmlOptions' => array(
											'class' => 'invoicecol-date',
										),
										'htmlOptions' => array(
											'width' => 80,
											'class' => 'invoicecol-date',
										),
									),
									'invoice_number' => array(
										'header' => 'Invoice #',
										'name' => 'invoice_number',
										'type' => 'raw',
										'value' => 'CHtml::link("#" . $data->invoice_number, array("invoice/view", "id" => $data->id), array("class" => "blue link"))',
										'headerHtmlOptions' => array(
											'class' => 'invoicecol-number',
										),
									),
									'case_number' => array(
										'header' => 'Related Transactions',
										'class' => 'common.widgets.grid.PsPartialViewColumn',
										'view' => '_column_case_number',
										'htmlOptions' => array(
											'class' => 'valign-top',
										)
									),
									'total' => array(
										'header' => 'Invoice Total',
										'name' => 'total',
										'type' => 'raw',
										'value' => 'Yii::app()->numberFormatter->formatCurrency($data->getInvoiceTotal(), $data->currency_code)',
										'headerHtmlOptions' => array(
											'class' => 'invoicecol-total',
										),
										'htmlOptions' => array(
											'class' => 'invoicecol-total',
											'width' => 80,
										),
									),
									'amount_applied' => array(
										'header' => 'Amount Applied',
										'type' => 'raw',
										'value' => 'Yii::app()->numberFormatter->formatCurrency($data->amount, $data->currency_code)',
										'headerHtmlOptions' => array(
											'class' => 'invoicecol-amount-applied',
										),
										'htmlOptions' => array(
											'class' => 'invoicecol-amount-applied',
											'width' => 50,
											'amount_applied' => ''
										),
									),
//							array(
//								'header' => 'Actions',
//								'headerHtmlOptions' => array(
//									'class' => 'action-column',
//								),
//								'class' => 'common.widgets.grid.PsActionColumn', // protected/components/PsActionColumn.php
//								'visible' => $containsTransactionItems,
//								'htmlOptions' => array(
//									'class' => 'action-column',
//									'width' => 80,
//								),
//								'options' => array(
//									'width' => 150,
//								),
//								'actions' => array(
//									'view' => array(
//										'label' => 'Calculate Commissions',
//										'icon' => '/images/icons/farmfresh/32x32/balance.png',
//										'class' => 'calculate-commissions',
//										'visible' => '(array_shift(array_values($data->invoiceItems(array("condition" => "property_case_id IS NOT NULL", "limit" => 1))))->propertyCase->property->prop_address_formatted != "")',
//										'url' => array('receipt/view', 'id' => ''),
//									),
//								)
//							),
								)
							));
							?>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>


